﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Service;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Controls.HtmlControls;

namespace Web.SysManage.Users
{
    public partial class UserManage : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(7);

            if (!Page.IsPostBack)
            {
                this.TextBind(null);
            }

            lmc.AddEditLinkUrl = "AdminEdit.aspx";
            lmc.EnableLinkPublish = false;
            lmc.EnableLinkOn = false;
            lmc.EnableLinkOff = false;
        }

        private void TextBind(UsersCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<UsersInfo> list = ServiceFactory.CreateInstance<UsersService>().GetModelList(ref ps, condition);

            this.rptList.DataSource = list;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            UsersInfo user = (UsersInfo)e.Item.DataItem;
            Anchor aUserName = (Anchor)e.Item.FindControl("aUserName");
            Span sNickName = (Span)e.Item.FindControl("sNickName");
            Span sIsLock = (Span)e.Item.FindControl("sIsLock");
            Span sAddDate = (Span)e.Item.FindControl("sAddDate");

            aUserName.Text = user.UserName;
            //if ((YesNo)CommonUtils.ToInt(user.IsLock) == YesNo.Yes)
            //{
            //    imgLock.Src = "../images/AdminLock.gif";
            //}
            //else
            //{
            //    imgLock.Src = "../images/AdminUnLock.gif";
            //}
            aUserName.Href = "UsersInfoView.aspx?UserID=" + CommonUtils.UrlEncode(user.UserID.ToString());
            sNickName.Text = user.NickName;
            //aAdminRole.Text = Row["RoleName"].ToString();
            //aAdminRole.Href = "adminrolemdy.aspx?roleid=" + CommonUtils.UrlEncode(Row["RoleID"].ToString());
            sAddDate.Text = !string.IsNullOrEmpty(user.AddDate.ToString()) ? user.AddDate.ToString("yyyy-MM-dd")
                : string.Empty;
        }
    }
}